
export const setStore = (name, content) => {
  // console.log(name + ':' + JSON.stringify(content))
	if (!name){return;}
	if (typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	window.localStorage.setItem(name, content);
};
export const getStore = name => {
	if (!name){return;}
	return window.localStorage.getItem(name);
};
export const removeStore = name => {
	if (!name){return;}
	window.localStorage.removeItem(name);
};

export const getStyle = (element, attr, NumberMode = 'int') => {
	let target;
	// scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
	if (attr === 'scrollTop') {
		target = element.scrollTop;
	} else if (element.currentStyle) {
		target = element.currentStyle[attr];
	} else {
		target = document.defaultView.getComputedStyle(element, null)[attr];
	}
	//在获取 opactiy 时需要获取小数 parseFloat
	if(NumberMode === 'float'){
		return parseFloat(target);
	}else{
		return parseInt(target ,10);
	}
};



export const loadMore = (element, callback) => {
	const windowHeight = window.screen.height;
	let height;
	let setTop;
	let paddingBottom;
	let marginBottom;
	let requestFram;
	let oldScrollTop;
	let docBody;

  const loadMoreNew = () => {

  	if (height + setTop + paddingBottom + marginBottom < windowHeight) {
  		return;
  	}else if(docBody + windowHeight >= height + setTop + paddingBottom + marginBottom){
  		callback();
  	}else{
      // 其它
    }

  };

  const moveEnd = () => {
  	requestFram = requestAnimationFrame(() => {

  		if (docBody !== oldScrollTop) {
  			 oldScrollTop = docBody;
			loadMoreNew();
  			 moveEnd();
  		} else {
  			cancelAnimationFrame(requestFram);
  			//为了防止鼠标抬起时已经渲染好数据从而导致重获取数据，应该重新获取dom高度
  			height = element.offsetHeight;
			loadMoreNew();
  		}
  	});
  };

	window.addEventListener('scroll', () => {
		docBody = document.documentElement.scrollTop || document.body.scrollTop;
	}, false);
	element.addEventListener('touchstart', () => {

		height = element.offsetHeight;
		setTop = element.offsetTop;
		paddingBottom = getStyle(element, 'paddingBottom');
		marginBottom = getStyle(element, 'marginBottom');
	}, {
		passive: true
	});
	element.addEventListener('touchend', () => {

		oldScrollTop = docBody;
		moveEnd();

	}, {
		passive: true
	});




};
export function formatDate(value) {
	let date = new Date(value);
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM;
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m;
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s;
	return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  }
  
  export function getData(day = 1, format = "{y}-{m}-{d} {h}:{n}:{s}") {
      const nDate = new Date();
      nDate.setDate(nDate.getDate() + day);
   
      const formatObj = {
        y: nDate.getFullYear(),
        m: nDate.getMonth() + 1,
        d: nDate.getDate(),
		h: nDate.getHours(),
		n: nDate.getMinutes(),
		s: nDate.getSeconds()
      };
      return format.replace(/{([ymdhns])+}/g, (result, key) => {
        const value = formatObj[key];
        return value.toString().padStart(2, "0");
      });
  }
/**
 * 显示返回顶部按钮，开始、结束、运动 三个过程中调用函数判断是否达到目标点
 */
export const showBack = callback => {
	let requestFram;
	let oldScrollTop;

  //判断是否达到目标点
  const showBackFun = () => {
  	if (document.body.scrollTop > 500) {
  		callback(true);
  	} else {
  		callback(false);
  	}
  };

  const moveEnd = () => {
  	requestFram = requestAnimationFrame(() => {
  		if (document.body.scrollTop !== oldScrollTop) {
  			oldScrollTop = document.body.scrollTop;
  			moveEnd();
  		} else {
  			cancelAnimationFrame(requestFram);
  		}
  		showBackFun();
  	});
  };


	document.addEventListener('scroll', () => {
		showBackFun();
	}, false);

	document.addEventListener('touchstart', () => {
		showBackFun();
	}, {
		passive: true
	});

	document.addEventListener('touchmove', () => {
		showBackFun();
	}, {
		passive: true
	});

	document.addEventListener('touchend', () => {
		oldScrollTop = document.body.scrollTop;
		moveEnd();
	}, {
		passive: true
	});




};

export const bodyHidden = isHide => {
	const el = document.getElementsByTagName('html')[0];
  function setStyle(elNew, strCss) {
  	var sty = elNew.style;
  	sty.cssText = sty.cssText + strCss;
  }
	if (isHide) {
		const cssStr = 'overflow-y: hidden;';
		setStyle(el, cssStr);
	} else {
		const cssStr = 'overflow-y: auto;';
		setStyle(el, cssStr);
	}




};

export const isToast = o => {
	if (o === true) {
		document.querySelector('#toastWrap').style.display = 'block';
		document.querySelector('#toast').style.display = 'block';
		bodyHidden(true);
	} else if (o === false) {
		document.querySelector('#toastWrap').style.display = 'none';
		document.querySelector('#toast').style.display = 'none';
		bodyHidden(false);
	} else {
		// 操作错误
	}
};

export const isObjVal = (objArr, objKey, objVal) => {

	let isVal = false;
	objArr.forEach((item, index) => {
		if (objKey) {
			if (item[objKey] === objVal) {
				isVal = true;
				return;
			}
		} else {
			if (item === objVal) {
				isVal = true;
				return;
			}
		}
	});
	return isVal;
};
export const throttle = (fn, delay = 500) => {
	let timer;
	return function (args) {
	  let _this = this;
	  if (timer) {
		return;
	  }
	  timer = setTimeout(() => {
		fn.call(_this, args);
		clearTimeout(timer);
		timer = null;
	  }, delay);
	};
  };